package com.liu.chain;

public abstract class Handler {
    private Handler nextHandler;
    private int level;

    protected void setNextHanlder(Handler handler) {
        this.nextHandler = handler;
    }

    //模板方法,判断请求级别和当前处理的级别
    public final String handlerRequest(Request request) {
        if (request.getLevel() == this.level)
            return response(request);
        else {
            if (this.nextHandler != null)
                return this.nextHandler.handlerRequest(request);
            else
                return "请求处理失败";
        }
    }

    protected int getLevel() {
        return this.level;
    }

    protected void setLevel(int level) {
        this.level = level;
    }

    //抽象处理方法，具体在子类中实现
    protected abstract String response(Request request);
}
